<?php

namespace app\api\controller;

use app\admin\model\About;
use app\common\model\Banner;
use app\common\controller\Api;
use app\common\model\Feedback;
use app\common\model\GoodsCategory;
use app\common\model\News;
use app\common\model\Order;
use app\common\model\User;
use Endroid\QrCode\QrCode;
use fast\Random;
use James\SolarCode\SolarCode;
use think\Db;

/**
 * 首页接口1
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/10/31   1:55 下午
     * 获取banner图列表
     */
    public function getBannerList(){
        $model=new Banner();
        $list=$model->whereNull('deletetime')->select();
        $this->success('请求成功',$list);
    }

    /**
     * 获取首页分类
     */
    public function getIndexCategoryList(){
        $model=new GoodsCategory();
//        $limit = input('limit',4);
        $list=$model->where('pid','=',0)->order('weigh desc')
//            ->limit($limit)
            ->select();
        $this->success('请求成功',['list'=>$list]);
    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/11/6   10:59 上午
     * 验证微信公众号
     */
    public function checkWxcode(){
        //获得参数 signature nonce token timestamp echostr
        $nonce     = $_GET['nonce'];
        $token     = 'k2M9WqNDBArsV1tIxDXEACxy3hJLjsiM';
        $timestamp = $_GET['timestamp'];
        $echostr   = $_GET['echostr'];
        $signature = $_GET['signature'];
        //形成数组，然后按字典序排序
        $array = array();
        $array = array($nonce, $timestamp, $token);
        sort($array);
        //拼接成字符串,sha1加密 ，然后与signature进行校验
        $str = sha1( implode( $array ) );
        if( $str == $signature && $echostr ){
            //第一次接入weixin api接口的时候
            echo  $echostr;
            exit;
        }

    }
    /**
     * 消息列表
     */
    public function getNew(){
        $user_id = $this->auth->id;
//        $type=input('type/d',1);
        $model=new News();
        $list['new1']=$model->where('type','=',1)->order('createtime desc')->find();
        $list['new2']=$model->where('type','=',2)->order('createtime desc')->find();
        $orderInfo = (new Order)->with(['order_goods'])->where('user_id','=',$user_id)->where('status', '=', 3)->order('sendtime desc')->find();
        if($orderInfo) {
            $list['new3']['title'] = '订单['.$orderInfo['order_goods'][0]['goods_name'].']已发货';
            $list['new3']['createtime'] = $orderInfo['sendtime'];
        } else {
            $list['new3']['title'] = '暂无新消息';
            $list['new3']['createtime'] = 0;
        }
        $this->success('成功',$list);
    }

    /**
     * 获取物流消息列表
     */
    public function getLogisticsNews()
    {
        $user_id = $this->auth->id;

        $rows = (new Order)->with(['order_goods'])
                            ->where('user_id', '=', $user_id)
                            ->where('status', '=', 3)
                            ->order('sendtime desc')
                            ->select();

        $data = [];
        foreach ($rows as $row) {
            $data[] = [
                'order_id' => $row['id'],
                'createtime' => $row['sendtime'],
                'title' => '您的订单待收货',
                'describe' => '订单['.$row['order_sn'].']已发货',
                'images' => $row['order_goods'][0]['images'],
            ];
        }

        $this->success('成功', ['list'=>$data, 'user_id'=>$user_id]);
    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/11/9   4:44 下午
     * 获取公告活动公告
     */
    public function getNewsList(){

        $type=input('type/d',1);
        $model=new News();
        $list=$model->where('type','=',$type)->order('createtime desc')->paginate();
        $this->success('成功',$list);
    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/11/10   10:25 上午
     * 获取公告详情
     */
    public function getNewsInfo(){
        $id=input('id/d');
        $model=new News();
        $list=$model->where('id','=',$id)->find();
        $this->success('成功',['info'=>$list]);
    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/12/4   下午5:53
     * 添加意见反馈
     */
    public function addFeedback(){
        $user_id=$this->auth->id;
        $content=input('content');
        $image=input('image');
        $model=new Feedback();
        if (!$content){
            $this->error('请完善内容');
        }
        if (!$image){
            $this->error('请完善图片');
        }
        $model->create(['content'=>$content,'image'=>$image,'user_id'=>$user_id]);
        $this->success('成功',null);
    }

    /**
     * @DESC:
     * @author: renyixu
     * @Time: 2020/12/4   下午6:10
     * 获取平台协议
     */
    public function getAbout(){
        $type=input('type/d');
        $model=new About();
        $info=$model->where('type','=',$type)->find();
        imageUrl($info['content']);
        $this->success('成功',$info);
    }
    /**
     * pc调起微信登陆
     * */
    public function wechat(){
        $notify = urlEncode("http://fubaoweb.191292.net/#/loginwx");
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".config('wetch.app_id')."&redirect_uri={$notify}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirec";
        $this->success('成功',$url);
    }
    /*
     * 客服电话
     * */
    public function kefu(){
        $model=new About();
        $info=$model->where('type','=',5)->find();
        $info['kefu'] = $info['content'];
        $this->success('成功',$info);

    }

    /*
     * 第三方登陆
     * */
    public function loginThree(){
        $uid = input('uid');
        //1 支付宝 2 微信openid  3 苹果 4 微信unionid 5 qq 登陆 ,6 小程序openid
        $type = input('type',1);
        if($uid=='false'|| $uid == null){
            $this->error('失败');
        }
        switch ($type){
            case 1:
                $feild = 'aliid';
                break;
            case 2:
                $feild = 'openid';
                break;
            case 3:
                $feild = 'iosid';
                break;
            case 4:
                $feild = 'unionid';
                break;
            case 5:
                $feild = 'qqid';
                break;
            case 6:
                $feild = 'xiao_openid';
                break;
        }
        $where[$feild]=$uid;
        $user = User::where($where)->find();

//        $type 0 未注册 1 未绑定手机号 2 未实名 3 正常登陆
        if(!empty($user)){
            //如果已经有账号则直接登录
            $ret = $this->auth->direct($user->id);
            if(!$user['mobile']){
                $type=1;
            }elseif (!$user['card']){
                $type=2;
            }else{
                $type=3;
            }
        }else{
            //不存在
            $ret = $this->auth->register($uid, Random::alnum(), '', '', [$feild=>$uid]);
            $type=1;
        }
        if ($ret) {
            $data = ['userinfo' => $this->auth->getUserinfo(),'type'=>$type];
            $this->success(__('成功'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }
    /**
     * 测试小程序码
     */
    public function testImg(){
        $aaa1=   getwxacode();
       dump($aaa1);
       die();
    }
}
